package leetcode;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class code226 {

    //创建一颗二叉树
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        //根据输入创建一棵二叉树
        Scanner scanner = new Scanner(System.in);
        String n = scanner.next();

        Integer[] nums = new Integer[n.toCharArray().length];
        for (int i = 0; i < n.toCharArray().length; i++) {
            nums[i] = n.toCharArray()[i] - '0';
        }
        TreeNode treeNode = buildTree(nums);

        invertTree(treeNode);
    }


    public static TreeNode buildTree(Integer[] arr) {
        if (arr == null || arr.length == 0 || arr[0] == null) {
            return null;
        }

        TreeNode root = new TreeNode(arr[0]);
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int i = 1;

        while (!queue.isEmpty() && i < arr.length) {
            TreeNode currentNode = queue.poll();

            // 处理左子节点
            if (i < arr.length && arr[i] != null) {
                currentNode.left = new TreeNode(arr[i]);
                queue.offer(currentNode.left);
            }
            i++;

            // 处理右子节点
            if (i < arr.length && arr[i] != null) {
                currentNode.right = new TreeNode(arr[i]);
                queue.offer(currentNode.right);
            }
            i++;
        }

        return root;
    }


    public static TreeNode invertTree(TreeNode root) {

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node != null) {
                TreeNode temp = node.left;
                node.left = node.right;
                node.right = temp;
                if (node.left != null)
                    queue.offer(node.left);
                if (node.right != null)
                    queue.offer(node.right);
            }
        }
        return root;
    }
}